package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsIssueMapper;
import com.qf.pojo.DtsIssue;
import com.qf.service.DtsIssueService;
import com.qf.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * projectName:workspace_6
 * author:hyn
 * time:2021/12/5 16:47
 * description:商品管理 关键词
 */
@Service
public class DtsIssueServiceImpl implements DtsIssueService {
    @Autowired
    private DtsIssueMapper issueMapper;
    //通用问题列表
    @Override
    public IPage<DtsIssue> findIssueAllList(String question, Integer page, Integer limit, String sort, String order) {
        IPage<DtsIssue> ipage = new Page(page,limit);
        QueryWrapper<DtsIssue> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(question)){
            wrapper.like("question",question);
        }
        wrapper.eq("deleted",false);
        if("asc".equals(order)){
            wrapper.orderByAsc(sort);
        }else{
            wrapper.orderByDesc(sort);
        }
        IPage<DtsIssue> p = issueMapper.selectPage(ipage, wrapper);
        return p;
    }
    //通用问题添加
    @Override
    public Object insert(DtsIssue issue) {
        LambdaQueryWrapper<DtsIssue> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsIssue::getQuestion,issue.getQuestion());
        if(issueMapper.selectOne(wrapper) != null){
            return ResponseUtil.fail(-1,"问题内容已存在");
        }
        issue.setAddTime(new Date());
        issue.setDeleted(false);
        return issueMapper.insert(issue) > 0 ? ResponseUtil.ok(issue) : ResponseUtil.fail(-1,"添加失败");
    }
    //通用问题修改
    @Override
    public Object update(DtsIssue issue) {
        if(!issueMapper.selectById(issue).getQuestion().equals(issue.getQuestion())){
            LambdaQueryWrapper<DtsIssue> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(DtsIssue::getQuestion,issue.getQuestion());
            if(issueMapper.selectOne(wrapper) != null){
                return ResponseUtil.fail(-1,"问题内容已存在");
            }
        }
        issue.setUpdateTime(new Date());
        issue.setDeleted(false);
        return issueMapper.updateById(issue) > 0 ? ResponseUtil.ok(issue) : ResponseUtil.fail(-1,"修改失败");
    }
    //通用问题删除
    @Override
    public Object delete(DtsIssue issue) {
        return issueMapper.deleteById(issue) > 0 ? ResponseUtil.ok(issue) : ResponseUtil.fail(-1,"删除失败");
    }
}
